package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.vo.AuthBindingInfoReviewVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.AuthBindingInfo;
import com.ruoyi.business.service.IAuthBindingInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业主认证Controller
 * 
 * @author asher
 * @date 2021-11-11
 */
@RestController
@RequestMapping("/business/auth_binding")
public class AuthBindingInfoController extends BaseController
{
    @Autowired
    private IAuthBindingInfoService authBindingInfoService;

    /**
     * 查询业主认证列表
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:list')")
    @GetMapping("/list")
    public TableDataInfo list(AuthBindingInfo authBindingInfo)
    {
        startPage();
        List<AuthBindingInfo> list = authBindingInfoService.selectAuthBindingInfoList(authBindingInfo);
        return getDataTable(list);
    }

    /**
     * 导出业主认证列表
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:export')")
    @Log(title = "业主认证", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AuthBindingInfo authBindingInfo)
    {
        List<AuthBindingInfo> list = authBindingInfoService.selectAuthBindingInfoList(authBindingInfo);
        ExcelUtil<AuthBindingInfo> util = new ExcelUtil<AuthBindingInfo>(AuthBindingInfo.class);
        return util.exportExcel(list, "业主认证数据");
    }

    /**
     * 获取业主认证详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:query')")
    @GetMapping(value = "/{bindingId}")
    public AjaxResult getInfo(@PathVariable("bindingId") Long bindingId)
    {
        return AjaxResult.success(authBindingInfoService.selectAuthBindingInfoByBindingId(bindingId));
    }

    /**
     * 新增业主认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:add')")
    @Log(title = "业主认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuthBindingInfo authBindingInfo)
    {
        // 检测小区是否认证过...
        List<AuthBindingInfo> authBindingInfos = authBindingInfoService.selectExistAuthBindingInfoByWxUserIdAndVillageId(authBindingInfo.getWxUserId(),
                authBindingInfo.getVillageId());
        if (authBindingInfos.size() > 0) {
            return AjaxResult.error("您已绑定该小区或正在认证中");
        }
        return toAjax(authBindingInfoService.insertAuthBindingInfo(authBindingInfo));
    }

    /**
     * 修改业主认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:edit')")
    @Log(title = "业主认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuthBindingInfo authBindingInfo)
    {
        return toAjax(authBindingInfoService.updateAuthBindingInfo(authBindingInfo));
    }

    /**
     * 删除业主认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth_binding:remove')")
    @Log(title = "业主认证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bindingIds}")
    public AjaxResult remove(@PathVariable Long[] bindingIds)
    {
        return toAjax(authBindingInfoService.deleteAuthBindingInfoByBindingIds(bindingIds));
    }

    /**
     * 申请业主认证
     */
    @Log(title = "申请业主认证", businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    public AjaxResult apply(@RequestBody AuthBindingInfo authBindingInfo)
    {
        // 检测小区是否认证过...
        List<AuthBindingInfo> authBindingInfos = authBindingInfoService.selectExistAuthBindingInfoByWxUserIdAndVillageId(authBindingInfo.getWxUserId(),
                authBindingInfo.getVillageId());
        if (authBindingInfos.size() > 0) {
            return AjaxResult.error("您已绑定该小区或正在认证中");
        }
        return toAjax(authBindingInfoService.applyAuthBindingInfo(authBindingInfo));
    }

    /**
     * 申请业主认证
     */
    @Log(title = "审批业主认证", businessType = BusinessType.INSERT)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody AuthBindingInfoReviewVO authBindingInfoReviewVO)
    {
        return toAjax(authBindingInfoService.reviewAuthBindingInfo(authBindingInfoReviewVO));
    }

    @GetMapping(value = "/getByWxUserId/{wxUserId}")
    public AjaxResult getByWxUserId(@PathVariable("wxUserId") Long wxUserId)
    {
        return AjaxResult.success(authBindingInfoService.selectAuthBindingInfoByWxUserId(wxUserId));
    }
}
